package com.comment.controller;

import com.comment.domain.Comments;
import com.comment.domain.Goods;
import com.comment.domain.Result;
import com.comment.service.GoodsService;
import com.comment.util.ExcelUtil;
import com.sun.corba.se.impl.ior.GenericIdentifiable;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.tools.ant.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {


    @Autowired
    private GoodsService goodsService;
    /**
     * 查看所有商品的评论
     */
    @RequestMapping("/findAllGoodsAndComments")
    private Result findAllGoodsAndComments(){
      return goodsService.findAllGoodsAndComments();
    }
    /**
    * 根据id查看一个商品的评论
    */
    @RequestMapping("/findOne")
    private Result findOne( Integer gId){
    return goodsService.findOne(gId);
}


    /**
     * 根据id查看一个名称
     */
    @RequestMapping("/findName")
    private Result findName( Integer gId){
        return goodsService.findName(gId);
    }

    /**
     * 添加评论
     */
    @RequestMapping("/addComment")
    private Result addComment(@RequestParam(value = "star",required = false) String star,@RequestParam(value = "comment",required = false)String comment,@RequestParam(value = "goodId",required = false)String goodId){
        HashMap hashMap = new HashMap();
        hashMap.put("star",star);
        hashMap.put("comment",comment);
        hashMap.put("goodId",goodId);
        return goodsService.addComment(hashMap);
    }
    /**
     * 评论导出
     */


    /**
     * 导出评论
     *
     */
    @RequestMapping(value = "/findAllExport")
    public String export(HttpServletRequest request, HttpServletResponse response ,Integer gId) throws Exception {
        String message = "";
        //获取数据
        Result result = goodsService.findOne(gId);
        List<Comments> list = (List<Comments>) result.getData();
        //excel标题
        String[] title = {"商品名","星级","评论"};
        //excel文件名
        Result name = goodsService.findName(gId);
        String data = (String) name.getData();
        String basename = new String(data.getBytes("gb2312"),"ISO8859-1");
        String fileName = basename+".xls";
        //sheet名
        String sheetName = data;
        String [][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            content[i][0]=data+"";
            Comments comments = list.get(i);
            String star = comments.getStar();
            String comment = comments.getComment();
            content[i][0]=data+"";
            content[i][1]=star+"星"+"";
            content[i][2]=comment+"";
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
            message = "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            message = "ERROR";
        }
        return null;
    }

    private void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"utf-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}